mruby 4.0.0
mruby is the lightweight implementation of the Ruby language
Loading...
Searching...
No Matches
error.h File Reference

More...

#include "common.h"
#include <string.h>
Include dependency graph for error.h:

Go to the source code of this file.

Classes

struct  RException
 mruby error handling. More...
struct  RBreak

Macros

#define MRB_EXC_EXIT   65536
#define MRB_EXC_EXIT_P(e)
#define MRB_EXC_EXIT_STATUS(mrb, e)
#define MRB_EXC_CHECK_EXIT(mrb, e)
#define mrb_exc_ptr(v)
#define mrb_exc_new_lit(mrb, c, lit)
#define MRB_USE_RBREAK_VALUE_UNION   1
#define RBREAK_VALUE_TT_MASK   ((1 << 8) - 1)
#define MRB_ENSURE(mrb, result_var, func, data)
 Calls func via mrb_protect_error() and then always executes the user block exactly once.

Typedefs

typedef mrb_value mrb_protect_error_func(mrb_state *mrb, void *userdata)
 Protect.

Functions

void mrb_sys_fail (mrb_state *mrb, const char *mesg)
mrb_value mrb_exc_new_str (mrb_state *mrb, struct RClass *c, mrb_value str)
void mrb_no_method_error (mrb_state *mrb, mrb_sym id, mrb_value args, const char *fmt,...)
static mrb_value mrb_break_value_get (struct RBreak *brk)
static void mrb_break_value_set (struct RBreak *brk, mrb_value val)
void mrb_clear_error (mrb_state *mrb)
 Error check.
mrb_bool mrb_check_error (mrb_state *mrb)
mrb_value mrb_protect_error (mrb_state *mrb, mrb_protect_error_func *body, void *userdata, mrb_bool *error)
 Protects a C function call from mruby exceptions.
mrb_value mrb_protect (mrb_state *mrb, mrb_func_t body, mrb_value data, mrb_bool *state)
 Protect (takes mrb_value for body argument).
mrb_value mrb_ensure (mrb_state *mrb, mrb_func_t body, mrb_value b_data, mrb_func_t ensure, mrb_value e_data)
 Ensure.
mrb_value mrb_rescue (mrb_state *mrb, mrb_func_t body, mrb_value b_data, mrb_func_t rescue, mrb_value r_data)
 Rescue.
mrb_value mrb_rescue_exceptions (mrb_state *mrb, mrb_func_t body, mrb_value b_data, mrb_func_t rescue, mrb_value r_data, mrb_int len, struct RClass **classes)
 Rescue exception.

Detailed Description

  • Exception class

See Copyright Notice in mruby.h

Macro Definition Documentation

◆ MRB_ENSURE

#define MRB_ENSURE ( mrb,
result_var,
func,
data )
Value:
for (mrb_bool MRB_UNIQNAME(_break_) = FALSE; \
!MRB_UNIQNAME(_break_) && \
(((result_var) = mrb_protect_error(mrb, func, data, &MRB_UNIQNAME(_break_))), \
((mrb)->exc = (MRB_UNIQNAME(_break_) ? mrb_obj_ptr((result_var)) : NULL)), \
TRUE); \
(void)(MRB_UNIQNAME(_break_) && (mrb)->jmp && (mrb_exc_raise(mrb, result_var), TRUE)), \
MRB_UNIQNAME(_break_) = TRUE)

Calls func via mrb_protect_error() and then always executes the user block exactly once.

Even if a global jump (similar to a Ruby exception) occurs within func, the block will be executed, and after the block's completion, the global jump will be re-thrown.

By checking mrb->exc != NULL within the block, you can determine if a global jump occurred in func.

If you want to suppress the global jump and continue processing, use mrb_clear_error(mrb); break;.

  • mrb: The mruby state reference
  • result_var: Pre-defined mrb_value type variable (to receive func's return value)
  • func: Function to call (compatible with mrb_protect_error_func)
  • data: User data to pass to func

Example:

mrb_value result;
MRB_ENSURE(mrb, result, body_func, userdata) {
  // This block is always executed (equivalent to Ruby's ensure)

  if (mrb->exc) {
    // Post-processing when an exception occurs
  }

  // To ignore the global jump, use `mrb_clear_error(mrb); break;` here
}

◆ MRB_EXC_CHECK_EXIT

#define MRB_EXC_CHECK_EXIT ( mrb,
e )
Value:
do {if (MRB_EXC_EXIT_P(e)) exit(MRB_EXC_EXIT_STATUS((mrb),(e)));} while (0)

◆ MRB_EXC_EXIT_P

#define MRB_EXC_EXIT_P ( e)
Value:
((e)->flags & MRB_EXC_EXIT)

◆ MRB_EXC_EXIT_STATUS

#define MRB_EXC_EXIT_STATUS ( mrb,
e )
Value:
((int)mrb_as_int((mrb),mrb_obj_iv_get((mrb),(e),MRB_SYM(status))))

◆ mrb_exc_new_lit

#define mrb_exc_new_lit ( mrb,
c,
lit )
Value:
mrb_exc_new_str(mrb, c, mrb_str_new_lit(mrb, lit))

◆ mrb_exc_ptr

#define mrb_exc_ptr ( v)
Value:
((struct RException*)mrb_ptr(v))
mruby error handling.
Definition error.h:18

Function Documentation

◆ mrb_ensure()

mrb_value mrb_ensure ( mrb_state * mrb,
mrb_func_t body,
mrb_value b_data,
mrb_func_t ensure,
mrb_value e_data )
extern

Ensure.

Implemented in the mruby-error mrbgem

◆ mrb_protect()

mrb_value mrb_protect ( mrb_state * mrb,
mrb_func_t body,
mrb_value data,
mrb_bool * state )
extern

Protect (takes mrb_value for body argument).

Implemented in the mruby-error mrbgem

◆ mrb_protect_error()

mrb_value mrb_protect_error ( mrb_state * mrb,
mrb_protect_error_func * body,
void * userdata,
mrb_bool * error )
extern

Protects a C function call from mruby exceptions.

This function executes a C function (body) within a protected environment. If an mruby exception occurs during the execution of body, this function catches the exception, sets the error flag, and returns the exception object. Otherwise, it returns the result of the body function and error remains FALSE.

This is crucial for calling mruby-related C functions from within C code that needs to handle potential mruby exceptions gracefully.

Parameters
mrbThe mruby state.
bodyA pointer to the C function to be executed. The function should have the signature: mrb_value func(mrb_state *mrb, void *userdata)
userdataA pointer to arbitrary data that will be passed to the body function.
errorA pointer to an mrb_bool that will be set to TRUE if an exception occurred, and FALSE otherwise. Can be NULL if not needed.
Returns
The value returned by the body function if no exception occurred, or the exception object if an exception occurred.

◆ mrb_rescue()

mrb_value mrb_rescue ( mrb_state * mrb,
mrb_func_t body,
mrb_value b_data,
mrb_func_t rescue,
mrb_value r_data )
extern

Rescue.

Implemented in the mruby-error mrbgem

◆ mrb_rescue_exceptions()

mrb_value mrb_rescue_exceptions ( mrb_state * mrb,
mrb_func_t body,
mrb_value b_data,
mrb_func_t rescue,
mrb_value r_data,
mrb_int len,
struct RClass ** classes )
extern

Rescue exception.

Implemented in the mruby-error mrbgem